package com.taomeng.service.impl;

import com.taomeng.constant.StatusConstant;
import com.taomeng.dao.LeaveDao;
import com.taomeng.dao.StudentDao;
import com.taomeng.dao.UserDao;
import com.taomeng.entity.*;
import com.taomeng.service.LeaveService;
import com.taomeng.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Slf4j
@Transactional
@Service
public class LeaveServiceImpl implements LeaveService {

    @Autowired
    private LeaveDao leaveDao;
    @Autowired
    private StudentDao studentDao;

    @Override
    public boolean add(LeaveApplication leaveApplication) {
        return leaveDao.add(leaveApplication);
    }

    @Override
    public PageBean<LeaveApplicationInfo> findAllByPage(int page, int limit, String status) {
        Subject subject = SecurityUtils.getSubject();
        User principal = (User) subject.getPrincipal(); //获取当前用户

        /*获取当前用户的角色
         * 班主任:只能看自己管理的班级请假信息
         * 辅导员：能看本学院的请假信息
         * 超管： 可以看全部的请假信息
         */

        if (subject.hasRole("banzhuren")) {
            return leaveDao.findAllByPage(page, limit, status, null, principal.getId());
        } else if (subject.hasRole("admin")) {
            return leaveDao.findAllByPage(page, limit, status, null, null);
        }
        return leaveDao.findAllByPage(page, limit, status, principal.getDepartmentId(), null);

        /*if (subject.hasRole("admin")) {
            return leaveDao.findAllByPage(page, limit, status, null, null);
        } else if (subject.hasRole("fudaoyuan") || subject.hasRole("deptadmin")) {
            return leaveDao.findAllByPage(page, limit, status, principal.getDepartmentId(), null);
        } else if (subject.hasRole("banzhuren")) {
            return leaveDao.findAllByPage(page, limit, status, null, principal.getId());
        }
        return null;*/

    }

    @Override
    public Integer audit(int applicationId, long teacherId, String opinion, int result) {
        Integer status = leaveDao.getStatusById(applicationId);
        Integer updateStatus = null;

        try {
            if (result == 1) {
                if (status.equals(StatusConstant.WAITING_FOR_APPROVAL)) {    //审批通过请假
                    leaveDao.updateStatus(applicationId, StatusConstant.WAITING_FOR_TERMINATE_LEAVE);  //修改状态为等待销假
                    updateStatus = StatusConstant.WAITING_FOR_TERMINATE_LEAVE;
                } else if (status.equals(StatusConstant.TERMINATE_LEAVE)) {  //审批通过销假
                    leaveDao.updateStatus(applicationId, StatusConstant.ARCHIVE);  //归档
                    updateStatus = StatusConstant.ARCHIVE;
                }else if (status.equals(StatusConstant.NOT_APPROVED)){//学生科销假
                    leaveDao.updateStatus(applicationId, StatusConstant.ARCHIVE);  //归档
                    updateStatus = StatusConstant.ARCHIVE;
                }
            } else if (result == 2) {   //审批不同意请假   直接归档
                if (status.equals(StatusConstant.WAITING_FOR_APPROVAL)) {
                    leaveDao.updateStatus(applicationId, StatusConstant.ARCHIVE);
                    updateStatus = StatusConstant.ARCHIVE;
                } else if (status.equals(StatusConstant.TERMINATE_LEAVE)) {
                    leaveDao.updateStatus(applicationId, StatusConstant.NOT_APPROVED); //申请销假审批不通过，需要去学生科
                    updateStatus = StatusConstant.NOT_APPROVED;
                }
            }
            leaveDao.addDetails(applicationId, teacherId, status, result, opinion);

            log.info(String.valueOf(updateStatus));
            return updateStatus;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return updateStatus;
    }

    @Override
    public List<LeaveApplicationDetail> findDetailByApplicationId(Integer applicationId) {
        return leaveDao.findDetailByApplicationId(applicationId);
    }

    @Override
    public List<LeaveApplication> findApplicationByStudentId(Integer id) {

        return leaveDao.findApplicationByStudentId(id);
    }

    @Override
    public List<LeaveApplication> findBacklogApplication(Integer id) {
       return leaveDao.findBacklogApplication(id);
    }

    @Override
    public Map<String, Object> findApplicationInfoById(int id,String studentNum) {
        Map<String, Object> map = leaveDao.findApplicationInfoById(id);
        Map<String, Object> studentInfo = studentDao.findAllInfoByStudentNum(studentNum);
        studentInfo.remove("id");
        map.putAll(studentInfo);
        return map;
    }

    @Override
    public boolean studentBack(Integer applicationId) {
        return leaveDao.studentBack(applicationId);
    }
}
